import { ApiResponse, PaginatedData, PaginatedResponse } from "../../../types/api-response.interface";
import { HttpStatus } from "@nestjs/common";

/**
 * 响应工具类
 * 提供统一的响应格式包装方法
 */
export class ResponseUtil {
  /**
   * 创建成功响应
   * @param data 响应数据
   * @param message 响应消息
   * @param code 状态码，默认200
   */
  static success<T>(data: T, message = "操作成功", code = HttpStatus.OK): ApiResponse<T> {
    return {
      code,
      success: true,
      data,
      message,
    };
  }

  /**
   * 创建失败响应
   * @param message 错误消息
   * @param code 错误状态码，默认400
   * @param data 错误数据，默认null
   */
  static error<T = null>(message = "操作失败", code = HttpStatus.BAD_REQUEST, data: T = null as T): ApiResponse<T> {
    return {
      code,
      success: false,
      data,
      message,
    };
  }

  /**
   * 创建分页成功响应
   * @param list 数据列表
   * @param total 总数量
   * @param page 当前页码
   * @param pageSize 每页数量
   * @param message 响应消息
   */
  static paginated<T>(
    list: T[],
    total: number,
    page: number,
    pageSize: number,
    message = "查询成功",
  ): PaginatedResponse<T> {
    const totalPages = Math.ceil(total / pageSize);

    return {
      code: HttpStatus.OK,
      success: true,
      data: {
        list,
        total,
        page,
        pageSize,
        totalPages,
      },
      message,
    };
  }

  /**
   * 创建无数据成功响应
   * @param message 响应消息
   */
  static noContent(message = "操作成功"): ApiResponse<null> {
    return {
      code: HttpStatus.NO_CONTENT,
      success: true,
      data: null,
      message,
    };
  }

  /**
   * 创建未找到响应
   * @param message 错误消息
   */
  static notFound(message = "资源未找到"): ApiResponse<null> {
    return {
      code: HttpStatus.NOT_FOUND,
      success: false,
      data: null,
      message,
    };
  }

  /**
   * 创建未授权响应
   * @param message 错误消息
   */
  static unauthorized(message = "未授权访问"): ApiResponse<null> {
    return {
      code: HttpStatus.UNAUTHORIZED,
      success: false,
      data: null,
      message,
    };
  }

  /**
   * 创建禁止访问响应
   * @param message 错误消息
   */
  static forbidden(message = "禁止访问"): ApiResponse<null> {
    return {
      code: HttpStatus.FORBIDDEN,
      success: false,
      data: null,
      message,
    };
  }

  /**
   * 创建服务器错误响应
   * @param message 错误消息
   */
  static serverError(message = "服务器内部错误"): ApiResponse<null> {
    return {
      code: HttpStatus.INTERNAL_SERVER_ERROR,
      success: false,
      data: null,
      message,
    };
  }
}
